;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; general settings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(global-set-key "\M-?" 'help-command)
(global-set-key "\C-h" 'delete-backward-char)
(defun kill-current-buffer ()
  (interactive)
  (kill-buffer (current-buffer)))
(global-set-key "\C-xk" 'kill-current-buffer)

;;;; alias for less keystrokes
(defalias 'cr 'comment-region)
(defalias 'ucr 'uncomment-region)
(defalias 'ab 'append-to-buffer)
(defalias 'af 'append-to-file)
(defalias 'sb 'speedbar)


(tool-bar-mode nil)
(scroll-bar-mode nil)
(setq column-number-mode t)                                          
;(global-linum-mode 1)
(fset 'yes-or-no-p 'y-or-n-p)
;; no sound for errors
(setq visible-bell t)
(setq ring-bell-function 'ignore)
;; no startup message
(setq inhibit-startup-message t)
(setq inhibit-splash-screen t)
;; a big kill ring for safety
(setq kill-ring-max 200)
(setq default-fill-column 60)
(setq default-tab-width 8)
;; don't use TAB to indent
(setq-default indent-tabs-mode nil)
(setq scroll-margin 3
      scroll-conservatively 10000)
(setq default-major-mode 'text-mode)
(show-paren-mode t)
(setq show-paren-style 'parentheses)
;(mouse-avoidance-mode 'animate)
(setq frame-title-format "%b")
;(auto-image-file-mode)
;; syntax highlight
(global-font-lock-mode t)
;; version control
(setq version-control nil)
;; (setq kept-new-versions 2)
;; (setq delete-old-versions t)
;; (setq kept-old-versions 1)
;; (setq dired-kept-versions 1)
(when (eq window-system 'x)
  (setq x-select-enable-clipboard t))
(setq require-final-newline t)
;; kills the whole line including its terminating newline
(setq kill-whole-line t)
;; color
(set-foreground-color "wheat")
(set-background-color "darkslategray")


(defun kill-buffer-when-command-exit ()
  "Close current buffer when `shell-command' exit."
  (let ((process (ignore-errors (get-buffer-process (current-buffer)))))
    (when process
      (set-process-sentinel
       process
       (lambda (proc state)
         (when (string-match "\\(finished\\|exited\\)" state)
           (kill-buffer (process-buffer proc))))))))
(add-hook 'shell-mode-hook 'kill-buffer-when-command-exit)
(add-hook 'gdb-mode-hook 'kill-buffer-when-command-exit)
(add-hook 'term-mode-hook 'kill-buffer-when-command-exit)
(defun kill-buffer-when-compile-success (process)
  "Close current buffer when `shell-command' exit."
  (set-process-sentinel process
                        (lambda (proc state)
                          (when (string-match "finished" state)
                            (delete-windows-on (process-buffer proc))))))
;; 编译成功后自动关闭 *compilation* buffer
(add-hook 'compilation-start-hook 'kill-buffer-when-compile-success)
;; another way
;; (setq compilation-finish-functions
;;       (lambda (buf str)
;;         (when (and (string (buffer-name buf) "*compilation*")
;;                    (not (string-match "exited abnormally" str)))
;;           (run-at-time 0.5 nil 'delete-windows-on buf))))

(defun swap-buffer-text ()
  "Swap the text of current buffer and its negibhour buffer"
  (interactive)
  (buffer-swap-text (window-buffer (next-window (selected-window)))))

(defun my-fullscreen ()
  (interactive)
  (x-send-client-message
   nil 0 nil "_NET_WM_STATE" 32
   '(2 "_NET_WM_STATE_FULLSCREEN" 0)))
;; 最大化
(defun my-maximized ()
  (interactive)
  (x-send-client-message
   nil 0 nil "_NET_WM_STATE" 32
   '(1 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
  (interactive)
  (x-send-client-message
   nil 0 nil "_NET_WM_STATE" 32
   '(1 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))
